package com.fbinggun.fattribute.listener;

import com.alibaba.fastjson.JSONObject;
import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import com.fbinggun.fattribute.data.PlayerData;
import com.fbinggun.fattribute.util.TempData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;

import java.sql.SQLException;

public class EntityUpdateListener implements Listener {


    //生成时加载属性
    @EventHandler(priority = EventPriority.HIGHEST)
    public void spawnEntity(CreatureSpawnEvent event) {
        LivingEntity entity = event.getEntity();
        if (entity.isDead()) {
            return;
        }
        DataManage.update(entity);
    }

    @EventHandler
    public void loreEntity(ChunkLoadEvent event) throws SQLException, ClassNotFoundException {
        Entity[] entityList = event.getChunk().getEntities();
        if (entityList != null) {
            for (Entity entity : entityList) {
                if (entity instanceof LivingEntity) {
                    LivingEntity livingEntity = (LivingEntity) entity;
                    String str = FAttributePlugin.tempTable.getJson(livingEntity.getUniqueId().toString());
                    if (str != null) {
                        TempData tempData = new TempData(JSONObject.parseObject(str));
                        tempData.loadEntityData(livingEntity);
                    } else {
                        DataManage.update(livingEntity);
                    }

                }
            }
        }
    }

    @EventHandler
    public void loreEntity(ChunkUnloadEvent event) throws SQLException, ClassNotFoundException {
        Entity[] entityList = event.getChunk().getEntities();
        if (entityList != null) {
            for (Entity entity : entityList) {
                if (entity instanceof LivingEntity) {
                    LivingEntity livingEntity = (LivingEntity) entity;
                    EntityData entityData = DataManage.getData(livingEntity);
                    if(entityData instanceof PlayerData){
                        continue;
                    }
                    TempData tempData = new TempData(entityData);
                    FAttributePlugin.tempTable.setJson(entityData.getUuid().toString(), tempData.toJson().toString());
                    DataManage.delData(livingEntity.getUniqueId());
                }
            }
        }
    }

    //死亡时删除 以防内存溢出
    @EventHandler
    public void onEntityDeathEvent(EntityDeathEvent event) throws SQLException, ClassNotFoundException {
        if (event instanceof Player) {
            return;
        }
        String str = FAttributePlugin.tempTable.getJson(event.getEntity().getUniqueId().toString());
        if (str != null) {
            FAttributePlugin.tempTable.delJson(event.getEntity().getUniqueId().toString());
        }
        DataManage.delData(event.getEntity().getUniqueId());

    }

    //玩家重生更新属性
    @EventHandler
    public void onEntityDeathEvent(PlayerRespawnEvent event) {
        EntityData entityData = DataManage.getData(event.getPlayer());
        if (entityData != null) {
            entityData.up();
        }
    }

    @EventHandler
    public void onPlayerItemHeldEvent(PlayerItemHeldEvent event) {
        Player player = event.getPlayer();
        EntityData entityData = DataManage.getData(player);
        if (entityData != null) {
            entityData.up();
        }
    }

    @EventHandler
    public void onPlayerItemHeldEvent(PlayerItemBreakEvent event) {
        Player player = event.getPlayer();
        EntityData entityData = DataManage.getData(player);
        if (entityData != null) {
            entityData.up();
        }
    }

    @EventHandler
    public void onPlayerItemHeldEvent(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        EntityData entityData = DataManage.getData(player);
        if (entityData != null) {
            entityData.up();
        }
    }

    @EventHandler
    public void onPlayerItemHeldEvent(PlayerQuitEvent event) {
        DataManage.delData(event.getPlayer().getUniqueId());
    }

    @EventHandler
    public void onPlayerItemHeldEvent(InventoryCloseEvent event) {
        Player player = (Player) event.getPlayer();
        EntityData entityData = DataManage.getData(player);
        if (entityData != null) {
            entityData.up();
        }
    }

    @EventHandler
    public void onPlayerItemHeldEvent(PlayerSwapHandItemsEvent event) {
        Player player = event.getPlayer();
        EntityData entityData = DataManage.getData(player);
        if (entityData != null) {
            entityData.up();
        }
    }
}

